<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开发者认证 - GSWS</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            line-height: 1.6;
        }

        .login-container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            max-width: 400px;
            width: 100%;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .lock-icon {
            font-size: 3rem;
            margin-bottom: 20px;
        }

        h1 {
            color: #2d3748;
            font-size: 1.8rem;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .subtitle {
            color: #718096;
            font-size: 1rem;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            color: #4a5568;
            font-weight: 600;
            margin-bottom: 8px;
        }

        input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: #ffffff;
        }

        input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            padding: 14px 20px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 1rem;
            border: none;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .error-message {
            background: linear-gradient(135deg, #fed7d7 0%, #fc8181 100%);
            color: #742a2a;
            padding: 12px 16px;
            border-radius: 10px;
            margin-bottom: 20px;
            border: 1px solid #fc8181;
            display: none;
        }

        .remember-me {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }

        .remember-me input[type="checkbox"] {
            width: auto;
            margin-right: 8px;
        }

        .remember-me label {
            margin-bottom: 0;
            font-weight: 400;
        }

        .footer {
            text-align: center;
            margin-top: 30px;
            color: #718096;
            font-size: 0.9rem;
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }

            h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="header">
            <div class="lock-icon">🔐</div>
            <h1>开发者认证</h1>
            <p class="subtitle">本网站还未正式上线，如需访问，请先认证</p>
        </div>

        <div class="error-message" id="error-message">
            用户名或密码错误，请重试
        </div>

        <form id="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required autocomplete="username">
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required autocomplete="current-password">
            </div>

            <div class="remember-me">
                <input type="checkbox" id="remember" name="remember">
                <label for="remember">记住登录状态</label>
            </div>

            <button type="submit" class="btn" id="login-btn">
                登录
            </button>
        </form>

        <div class="footer">
            <p>Powered by GSWS</p>
        </div>
    </div>

    <script>
        // 检查localStorage中是否有保存的凭据
        window.addEventListener('DOMContentLoaded', function() {
            const savedCredentials = localStorage.getItem('dev_credentials');
            if (savedCredentials) {
                try {
                    const credentials = JSON.parse(savedCredentials);
                    document.getElementById('username').value = credentials.username || '';
                    document.getElementById('password').value = credentials.password || '';
                    document.getElementById('remember').checked = true;
                } catch (e) {
                    console.error('Failed to parse saved credentials:', e);
                }
            }
        });

        // 处理表单提交
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const remember = document.getElementById('remember').checked;
            const loginBtn = document.getElementById('login-btn');
            const errorMessage = document.getElementById('error-message');

            // 禁用登录按钮
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';

            // 生成token
            const token = btoa(username + ':' + password);

            // 发送认证请求
            fetch('/dev-auth', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'dev_token=' + encodeURIComponent(token)
            })
            .then(response => {
                if (response.ok) {
                    // 如果选择记住登录状态，保存到localStorage
                    if (remember) {
                        localStorage.setItem('dev_credentials', JSON.stringify({
                            username: username,
                            password: password
                        }));
                    } else {
                        localStorage.removeItem('dev_credentials');
                    }

                    // 登录成功，重定向到原始请求的页面或根目录
                    const returnUrl = new URLSearchParams(window.location.search).get('return') || '/';
                    window.location.href = returnUrl;
                } else {
                    throw new Error('Authentication failed');
                }
            })
            .catch(error => {
                console.error('Login error:', error);
                errorMessage.style.display = 'block';

                // 3秒后隐藏错误消息
                setTimeout(() => {
                    errorMessage.style.display = 'none';
                }, 3000);
            })
            .finally(() => {
                // 重新启用登录按钮
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            });
        });

        // 输入时隐藏错误消息
        document.getElementById('username').addEventListener('input', function() {
            document.getElementById('error-message').style.display = 'none';
        });

        document.getElementById('password').addEventListener('input', function() {
            document.getElementById('error-message').style.display = 'none';
        });
    </script>
</body>
</html>